package cn.wgx.modules.sys.util;

import cn.wgx.commons.util.SpringContextHolder;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.sys.entity.SysMsg;
import cn.wgx.modules.sys.services.SysMsgService;
import cn.wgx.modules.sys.services.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 消息工具类
 */
@Component
@Slf4j
public class SysMsgUtil {

    @Autowired
    private SysUserService sysUserService;

    private static SysMsgService sysMsgService = SpringContextHolder.getBean("sysMsgService");



    public static int sendMsgNoMQ(String title, String content, Integer type, Integer toUserId, Integer fromUser_id) {
        if(Validation.isNullOrZero(toUserId) || Validation.isNullOrZero(fromUser_id)
                || StringUtils.isBlank(title) || StringUtils.isBlank(content)){
            return 0;
        }
        SysMsg sysMsg = new SysMsg();
        sysMsg.setTitle(title);
        sysMsg.setContent(content);
        sysMsg.setToUserId(toUserId);
        sysMsg.setFromUserId(fromUser_id);
        sysMsg.setType(type);
        return sysMsgService.insertOne(sysMsg, false);
    }

    private static boolean validParam(SysMsg sysMsg){
        if(sysMsg == null){return false;}
        if(sysMsg.getSchoolCode() == null && sysMsg.getSquadCode() == null && sysMsg.getToUserId() == null){
            return false;
        }

        return true;
    }


//    private static class SysMsgSender extends Thread {
//        SysMsg sysMsg;
//
//        public SysMsgSender(SysMsg sysMsg) {
//            this.sysMsg = sysMsg;
//        }
//
//        @Override
//        public void run() {
//            sysMsgService.insertOne(sysMsg);
//        }
//    }
}
